package baidu.com.matedemo.network;

import android.view.View;
import android.widget.AdapterView;

import com.baidu.duer.libcore.adapter.CommonAdapter;
import com.baidu.duer.libcore.adapter.ViewHolder;
import com.baidu.duer.libcore.util.ToastUtil;
import com.baidu.duer.smartmate.duerlink.bean.DuerlinkError;
import com.baidu.duer.smartmate.duerlink.discovery.DuerlinkDiscoveryManager;
import com.baidu.duer.smartmate.duerlink.discovery.IDuerlinkLanDiscoveryListener;
import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.out.oauth.IResponseCallback;

import java.util.ArrayList;
import java.util.List;

import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.R;
import baidu.com.matedemo.base.ListBaseFragment;
import baidu.com.matedemo.pair.DevicePairWrapper;

/**
 * Created by sunjian11 on 17/11/10.
 * 局域网发现并授权
 */

public class LanDiscoveryFragment extends ListBaseFragment implements AdapterView.OnItemClickListener {
    private DuerlinkDiscoveryManager mDiscoveryManager = null;
    private List<DuerDevice> mDuerDeviceList = new ArrayList<>();
    private CommonAdapter<DuerDevice> mAdapter = null;
    private DevicePairWrapper pairWrapper = null;

    @Override
    public void onViewCreated(View view) {
        mDiscoveryManager = new DuerlinkDiscoveryManager(getMActivity());
        mAdapter = new CommonAdapter<DuerDevice>(getMActivity(), R.layout.item_function, mDuerDeviceList) {
            @Override
            protected void convert(ViewHolder viewHolder, DuerDevice duerDevice, int i) {
                viewHolder.setText(R.id.text_view, duerDevice.getDeviceId());
            }
        };
        setAdapter(mAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDiscoveryManager = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final DuerDevice device = (DuerDevice) parent.getItemAtPosition(position);
        if (device == null) {
            ToastUtil.toastWarnning(getMActivity(), "device is null");
            return;
        }

        if (DemoApp.getInstance().getOauthType() == null) {
            DemoApp.getInstance().addDuerDevice(device);
            DemoApp.getInstance().setCurrentDevice(device);
            getMActivity().finish();
            return;
        }

        // 配对
        showProgressBar();
        IResponseCallback callback = new IResponseCallback() {
            @Override
            public void onSuccess() {
                dismissProgressBar();
                // 跳转到授权页面
                DemoApp.getInstance().addDuerDevice(device);
                DemoApp.getInstance().setCurrentDevice(device);
                ToastUtil.toastSuccess(getMActivity(), "帐号绑定成功");
                getMActivity().finish();
            }

            @Override
            public void onError(long status, String msg) {
                dismissProgressBar();
                ToastUtil.toastWarnning(getMActivity(), "授权失败：" + msg);
            }
        };
        pairWrapper = new DevicePairWrapper();
        pairWrapper.startPair(getMActivity(), DemoApp.getInstance().getOauthType(), device, callback);
    }

    @Override
    public void onStart() {
        super.onStart();
        mDuerDeviceList.clear();
        if (mDiscoveryManager != null) {
            mDiscoveryManager.startLanDiscovery(mDiscoveryListener);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mDiscoveryManager != null) {
            mDiscoveryManager.stopLanDiscovery();
        }
        if (pairWrapper != null) {
            pairWrapper.stopPair();
        }
    }

    private IDuerlinkLanDiscoveryListener mDiscoveryListener = new IDuerlinkLanDiscoveryListener() {
        @Override
        public void onDiscovery(DuerDevice device) {
            mDuerDeviceList.add(device);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onDiscoveryComplete(List<DuerDevice> devices) {
        }

        @Override
        public void onDiscoveryFail(DuerlinkError error) {
        }
    };
}
